package com.likou;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Author: 陈桂明
 * Date: 2025/4/10 22:35
 * Description: q438
 */
public class q438 {
    public static void main(String[] args) {
        List<Integer> anagrams = new q438().findAnagrams("cbaebabacd", "abc");
        System.out.println(anagrams);
    }

    public List<Integer> findAnagrams(String s, String p) {
        if (s.length() < p.length()) {
            return new ArrayList<>();
        }

        char[] chars1 = p.toCharArray();
        Arrays.sort(chars1);
        String s1 = new String(chars1);
        int l = s1.length();

        List<Integer> list = new ArrayList<>();
        for (int left = 0, right = l - 1; right < s.length(); left++, right++) {
            char[] chars2 = s.substring(left, right + 1).toCharArray();
            Arrays.sort(chars2);
            String s2 = new String(chars2);

            while (!s1.equals(s2)) {
                left++;
                right++;
                if (right == s.length()) {
                    return list;
                }
                chars2 = s.substring(left, right + 1).toCharArray();
                Arrays.sort(chars2);
                s2 = new String(chars2);
            }

            list.add(left);
        }

        return list;
    }
}
